//
//  CCMMineVC.swift
//  CCMoney_swift
//
//  Created by JKHuang on 2018/11/22.
//  Copyright © 2018 JKHuang. All rights reserved.
//

import UIKit

fileprivate let kCCMMineViewCell = "CCMMineViewCell"
fileprivate let kCCMMineHeadViewCell = "CCMMineHeadViewCell"
fileprivate let kCCMMineOrderViewCell = "CCMMineOrderViewCell"
fileprivate let kCCMMineOrderItemViewCell = "CCMMineOrderItemViewCell"

class CCMMineVC: CCMBaseViewController {

    fileprivate lazy var TableView:UITableView = {
        let tableView = UITableView(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: ScreenHeight), style: .plain)
        tableView.separatorStyle = .none
        tableView.backgroundColor = MainColor
        tableView.register(CCMMineViewCell.self, forCellReuseIdentifier: kCCMMineViewCell)
        tableView.register(CCMMineHeadViewCell.self, forCellReuseIdentifier: kCCMMineHeadViewCell)
        tableView.register(CCMMineOrderViewCell.self, forCellReuseIdentifier: kCCMMineOrderViewCell)
        tableView.register(CCMMineOrderItemViewCell.self, forCellReuseIdentifier: kCCMMineOrderItemViewCell)
        tableView.delegate = self
        tableView.dataSource = self
        return tableView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.hiddenCustomBack = true
        setUI()
        // Do any additional setup after loading the view.
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.navigationBar.isHidden = true
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.navigationBar.isHidden = false
    }
}

extension CCMMineVC{
    
    fileprivate func setUI() {
        view.addSubview(self.TableView)
        self.TableView.snp.makeConstraints { (make) in
            make.edges.equalTo(self.view)
        }
    }
}

extension CCMMineVC:UITableViewDelegate, UITableViewDataSource{
    
    func numberOfSections(in tableView: UITableView) -> Int {
        
        return 2
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        if section == 0 {
            
            return 3
        }else{
            
            return 2
        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        switch indexPath.section {
            
        case 0:
            if indexPath.row == 0{
                let cell = tableView.dequeueReusableCell(withIdentifier: kCCMMineHeadViewCell, for: indexPath) as!CCMMineHeadViewCell
                cell.selectionStyle = .none
                return cell;
            }else if indexPath.row == 1 {
                let cell = tableView.dequeueReusableCell(withIdentifier: kCCMMineOrderViewCell, for: indexPath) as!CCMMineOrderViewCell
                cell.selectionStyle = .none
                return cell
            }
            
            let cell = tableView.dequeueReusableCell(withIdentifier: kCCMMineOrderItemViewCell, for: indexPath) as!CCMMineOrderItemViewCell
            return cell
            
        default:
            
            let cell = tableView.dequeueReusableCell(withIdentifier: kCCMMineViewCell, for: indexPath) as!CCMMineViewCell
            cell.selectionStyle = .none
            if indexPath.row == 0{
                
                cell.titleLable.text = "地址管理"
                cell.lineView.isHidden = false
            }else{
               
                cell.titleLable.text = "客服"
                cell.lineView.isHidden = true
            }
            return cell
        }
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        switch indexPath.section {
        case 0:
            if indexPath.row == 0{
                
                return 134
            }else if indexPath.row == 1{
                
                return 45
            }
            return 80
            
        default:
            
            return 60
        }
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        
        return 10
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        
        let view = UIView(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: 10))
        view.backgroundColor = MainColor
        return view
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.section == 0 {
            if indexPath.row == 0{
                let vc = CCMMineSettingVC()
                vc.hidesBottomBarWhenPushed = true
                self.navigationController?.pushViewController(vc, animated: true)
            }
        }
    }
}
